//
//  SportVC.swift
//  JTYP
//
//  Created by Groot on 2017/11/23.
//  Copyright © 2017年 inspur. All rights reserved.
//

import UIKit

class SportVC: TransparentNavBarVC {
    
    // 顶部工具栏
    @IBOutlet weak var topView: UIView!
    // 底部视图
    @IBOutlet weak var bottomView: UIView!
    
    let scrollBarItems = [ScrollBarItem.init(itemName: "走路"),
                          ScrollBarItem.init(itemName: "跑步")]
    
    // 顶部工具栏: 天气
    private lazy var weatherView: WeatherView = {
        let view: WeatherView =  Bundle.main.loadNibNamed("WeatherView", owner: nil, options: nil)?.first as! WeatherView
        return view
    }()
    // 顶部工具栏: 消息
    private lazy var messageIconBtn: UIButton = {
        let btn: UIButton = UIButton.init(type: .system)
        btn.setBackgroundImage(UIImage.init(named: "sport_top_message"), for: .normal)
        return btn
    }()
    // 成就
    private lazy var achievementView: AchievementView = {
       let view = AchievementView()
        view.backgroundColor = .red
        return view
    }()
    
    // 滚动消息
    private lazy var scrollNewsView: ScrollNewsView = {
        let view = Bundle.main.loadNibNamed("ScrollNewsView", owner: nil, options: nil)?.first as! ScrollNewsView
        return view
    }()
    
    // 今日排行
    private lazy var todayRankingView: RankingView = {
        let view = Bundle.main.loadNibNamed("RankingView", owner: nil, options: nil)?.first as! RankingView
        return view
    }()
    // 月度排行
    private lazy var monthRankingView: RankingView = {
        let view = Bundle.main.loadNibNamed("RankingView", owner: nil, options: nil)?.first as! RankingView
        return view
    }()
    
    @IBOutlet weak var scrollBarView: ScrollBarView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.white
        
        // 顶部天气
        topView.addSubview(weatherView)
        weatherView.snp.makeConstraints { (make) in
            make.leading.equalToSuperview().offset(8)
            make.top.equalToSuperview().offset(35)
            make.width.greaterThanOrEqualTo(90)
            make.height.greaterThanOrEqualTo(20)
        }
        // 顶部消息
        topView.addSubview(messageIconBtn)
        messageIconBtn.snp.makeConstraints { (make) in
            make.trailing.equalToSuperview().offset(-12)
            make.top.equalToSuperview().offset(35)
            make.width.height.equalTo(25)
        }
        // 成就
        bottomView.addSubview(achievementView)
        achievementView.snp.makeConstraints { (make) in
            make.leading.trailing.top.equalToSuperview()
            make.height.equalTo(AchievementView.viewHeight)
        }
        // 滚动消息
        bottomView.addSubview(scrollNewsView)
        scrollNewsView.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(ScrollNewsView.height)
        }
        // 今日排行
        bottomView.addSubview(todayRankingView)
        bottomView.addSubview(monthRankingView)
        todayRankingView.snp.makeConstraints { (make) in
            make.top.equalTo(scrollNewsView.snp.bottom)
            make.leading.equalToSuperview()
            make.trailing.equalToSuperview()
            make.height.equalTo(monthRankingView.snp.height)
        }
        todayRankingView.rank = Ranking.init(title: "今日排行", value: "-", iconName: "sport_jinri")
        // 月度排行
        monthRankingView.snp.makeConstraints { (make) in
            make.top.equalTo(todayRankingView.snp.bottom)
            make.leading.equalToSuperview()
            make.trailing.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        monthRankingView.rank = Ranking.init(title: "月度排行", value: "-", iconName: "sport_saishi")
        
        scrollBarView.layoutIfNeeded()
        
        let vc1 = PedometerBaseVC()
        vc1.listener = self
        let vcs = [vc1,
                   PedometerBaseVC()]
        
        scrollBarView.normalTitleColor = .white
        scrollBarView.selectedTitleColor = .white
        scrollBarView.scrollBarTitleFontSize = 17.0
        scrollBarView.setScrollBarItems(items: scrollBarItems, and: vcs, style: .average)
        scrollBarView.backgroundColor = .clear
        scrollBarView.scrollBar.backgroundColor = .clear
        scrollBarView.scrollBar.snp.remakeConstraints { (make) in
            make.top.equalToSuperview()
            make.height.equalTo(35)
            make.centerX.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.5)
        }
        scrollBarView.scrollBar.layoutIfNeeded()
        scrollBarView.updateScrollBarConstraints()
    }
}
extension SportVC: PedometerListener {
    func pedometerChanged(pedometer: Int) {
        achievementView.pedometer = pedometer
    }
}
